package com.oa.modules.approvalsec1.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.dao.AssistInManagingDao;
import com.oa.modules.approvalsec1.entity.AssistInManagingEntity;
import com.oa.modules.approvalsec1.service.AssistInManagingService;
import com.oa.modules.sys.entity.SysUserTokenEntity;
import com.oa.modules.sys.service.SysCaptchaService;
import com.oa.modules.sys.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
//import com.oa.modules.approvalsec1.wx.HttpUtil;

import java.io.File;
import java.util.*;

@Service("assistInManagingService")
public class AssistInManagingServiceImpl extends ServiceImpl<AssistInManagingDao, AssistInManagingEntity> implements AssistInManagingService {

    @Value("${api.url}")
    private String URL;

    @Autowired
    private SysUserTokenService userTokenService;

    @Autowired
    private SysCaptchaService captchaService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        params.put("status", Constant.YES_OR_NO.YES.getValue());
        IPage page = AppUtils.getPage(new Query<AssistInManagingEntity>(), params);
        return AppUtils.setPage(page, baseMapper.findPage(params), baseMapper.findRecords(page, params));
    }

    @Override
    public List<AssistInManagingEntity> queryIsAssign() {
        return baseMapper.queryIsAssign();
    }

    /**
     * 获取token
     *
     * @return
     */
    @Override
    public String getToken() {
        SysUserTokenEntity userToken = userTokenService.getById(90909090);
        long between = DateUtil.between(userToken.getExpireTime(), new Date(), DateUnit.SECOND);
        if (between >= 3600) {
            String response = HttpUtil.sendPost(URL + "/frontdesk/userInfo", null);
            Map map = new Gson().fromJson(response, Map.class);
            if ((Double) map.get("code") == 0) {
                Map<String, String> data = (Map<String, String>) map.get("data");
                String token = data.get("token");
                userToken.setToken(token);
                userToken.setExpireTime(new Date());
                userTokenService.updateById(userToken);
                return token;
            }
        } else {
            return userToken.getToken();
        }
        return null;
    }

    /**
     * 获取手机验证码
     *
     * @return
     */
    @Override
    public void sendSms(String phoneNumber) {

        Gson gson = new Gson();
        String token = getToken();
        Map params = new HashMap<String, String>();
        params.put("phoneNumber", phoneNumber);
        String response = HttpRequest.post(URL + "/api/frontdesk/sendSms")
                .form(params)
                .header("x-auth-token", token)
                .execute()
                .body();
    }

    /**
     * 获取区域
     *
     * @return
     */
    @Override
    public List<Map> getArea() {
        Gson gson = new Gson();
        String token = getToken();
        String response = HttpRequest.post(URL + "/api/agent/belongArea")
                .header("x-auth-token", token)
                .execute()
                .body();
        Map result = gson.fromJson(response, Map.class);
        List<Map> data = (List<Map>) result.get("data");
        return data;
    }

    /**
     * 帮办申请
     *
     * @param
     */
    @Override
    public Map attacheApplication(AssistInManagingEntity ass) {
        String token = getToken();
        Gson gson = new Gson();
        HashMap<String, Object> body = new HashMap<>();
        body.put("agentType", ass.getApplicatType());
        body.put("belongArea", ass.getAddress());
        body.put("companyName", ass.getCompanyName());
        body.put("username", ass.getRealName());
        body.put("agentDesc", ass.getRequirement());
        body.put("phoneNumber", ass.getContactPhone());
        body.put("validCode", ass.getCaptcha());
        body.put("source", "eqiban");
        String[] filePaths = ass.getFileUrl().split(",");
        ArrayList<File> files = new ArrayList();
        if (filePaths != null && filePaths.length > 0 && !"".equals(filePaths[0])) {
            for (int i = 0; i < filePaths.length; i++) {
                String localPath = filePaths[i].replaceAll("https://www.qhspj.com//examination-approval-project", "/home/image/examination-approval-project");
                String ipath = localPath.substring(0, localPath.lastIndexOf('/'));
                files.add(new File(ipath));
            }
            body.put("file", files);
        }
        String response = HttpRequest.post(URL + "/api/agent/submitApply")
                .form(body)
                .header("x-auth-token",token)
                .execute()
                .body();
        Map result = gson.fromJson(response, Map.class);
        return result;
    }


}
